package com.txt.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Random;

public class TxtInterceptor implements AsyncHandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws IOException {
        Random random = new Random();
        int i = random.nextInt(10);
        if(i>1)
        {
            return true;
        }
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.getWriter().print("error");
        return false;
    }
}
